package com.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bean.PurchaseOrder;
import com.bean.PurchaseOrderMaterial;
import com.bean.Stock;
import com.bean.User;
import com.service.PurchaseOrderMaterialService;
import com.service.PurchaseOrderService;
import com.service.StockService;
import com.util.TimeUtil;

/**
 * @author author : 张鹏
 * @date createTime：2018年1月25日 下午5:49:09
 * @Description: 该类的功能描述
 */
@Controller
@RequestMapping("purchaseOrder")
public class PurchaseOrderController {
	@Autowired
	private PurchaseOrderService orderService;

	@Autowired
	private PurchaseOrderMaterialService orderMaterialService;

	@Autowired
	private StockService stockService;

	private PurchaseOrder purchaseOrder;

	/**
	 * 获取所有未删除的采购订单
	 * 
	 * @return
	 */
	@RequestMapping("getAllPurchaseOrderAjax")
	@ResponseBody
	public List<PurchaseOrder> getAllPurchaseOrderAjax() {
		return orderService.selectAll();
	}

	/**
	 * 获得数据分析的图表数据
	 * 
	 * @return
	 */
	@RequestMapping("getPurchaseOrderChartsDataAjax")
	@ResponseBody
	public List<PurchaseOrderMaterial> getPurchaseOrderChartsDataAjax() {
		return orderService.getSumMaterialQuantity();
	}

	/**
	 * 根据主键返回采购订单 及其采购订单的商品
	 * 
	 * @param purchaseOrderId
	 *            主键
	 * @param remark
	 *            标记 exame代表审核页面 show代表查看页面
	 * @param returnTo
	 * @param request
	 * @return
	 */
	@RequestMapping("getPurchaseOrderByPrimaryKeyReturnTo")
	public String getPurchaseOrderByPrimaryKeyReturnTo(int purchaseOrderId,
			String remark, String returnTo, HttpServletRequest request) {
		request.setAttribute("purchaseOrder",
				orderService.selectByPrimaryKey(purchaseOrderId));
		request.setAttribute("purchaseOrderMaterialList",
				orderMaterialService.selectByPurchaseOrderId(purchaseOrderId));

		request.setAttribute("remark", remark);
		return returnTo;
	}

	/**
	 * 创建采购订单
	 * 
	 * @param supplierId
	 * @param purchaseTime
	 * @param totalPrice
	 * @param createById
	 * @param remark
	 * @param materialList
	 * @return
	 */
	@RequestMapping("createNewPurchaseOrderAjax")
	@ResponseBody
	public int createNewPurchaseOrderAjax(int supplierId, String purchaseTime,
			Double totalPrice, int createById, String remark,
			String materialList,HttpServletRequest request) {
		int userId=((User)request.getSession().getAttribute("userInfo")).getUserId();
		purchaseOrder = new PurchaseOrder();
		purchaseOrder.setSupplierId(supplierId);
		purchaseOrder.setPurchaseTime(purchaseTime);
		purchaseOrder.setTotalPrice(totalPrice);
		purchaseOrder.setCreateById(userId);
		purchaseOrder.setCreateTime(TimeUtil.formatTime());
		purchaseOrder.setRemark(remark);
		purchaseOrder.setFinish(0);
		purchaseOrder.setState("1");
		int result = orderService.insert(purchaseOrder);
		int purchaseOrderId = purchaseOrder.getPurchaseOrderId();

		if (!"".equals(materialList) && materialList != null
				&& !"[]".equals(materialList)) {
			PurchaseOrderMaterial material;
			JSONArray list = JSONArray.fromObject(materialList);
			JSONObject jobj;
			for (int i = 0; i < list.size(); i++) {
				material = new PurchaseOrderMaterial();
				jobj = (JSONObject) list.get(i);

				material.setPurchaseOrderId(purchaseOrderId);
				material.setMaterialId(jobj.getInt("materialId"));
				material.setTotalPrice(jobj.getDouble("totalPrice"));
				material.setMaterialQuantity(jobj.getString("materialQuantity"));
				orderMaterialService.insert(material);
			}
		}

		return result;
	}

	/**
	 * 审核
	 * 
	 * @param purchaseOrderId
	 * @param exameSuggestion
	 * @param state
	 * @return
	 */
	@RequestMapping("examePurchaseOrderAjax")
	@ResponseBody
	public int examePurchaseOrderAjax(int purchaseOrderId,
			String exameSuggestion, String state,HttpServletRequest request) {
		int userId=((User)request.getSession().getAttribute("userInfo")).getUserId();
		purchaseOrder = new PurchaseOrder();
		purchaseOrder.setPurchaseOrderId(purchaseOrderId);
		purchaseOrder.setExameById(userId);
		purchaseOrder.setExameTime(TimeUtil.formatTime());
		purchaseOrder.setExameSuggestion(exameSuggestion);
		purchaseOrder.setState(state);

		// 设置流程是否结束的状态
		if ("3".equals(state)) {
			purchaseOrder.setFinish(1);
		} else {
			purchaseOrder.setFinish(0);
		}
		return orderService.exame(purchaseOrder);
	}

	/**
	 * 根据状态得到的采购订单 比如代发货 待收货
	 * 
	 * @return
	 */
	@RequestMapping("getPurchaseOrderListByStateAjax")
	@ResponseBody
	public List<PurchaseOrder> getPurchaseOrderListByStateAjax(int state) {
		return orderService.selectByState(state);
	}

	/**
	 * 根据状态码和供应商查询
	 * 
	 * @param state
	 * @return
	 */
	@RequestMapping("getPurchaseOrderListByStateAndSupplierIdAjax")
	@ResponseBody
	public List<PurchaseOrder> getPurchaseOrderListByStateAndSupplierIdAjax(
			int state,HttpServletRequest request) {
		int userId=((User)request.getSession().getAttribute("userInfo")).getUserId();
		purchaseOrder = new PurchaseOrder();
		purchaseOrder.setState(state + "");
		purchaseOrder.setSupplierId(userId);
		return orderService.selectByStateAndSupplierId(purchaseOrder);
	}

	@RequestMapping("getPurchaseOrderListByFinishAjax")
	@ResponseBody
	public List<PurchaseOrder> getPurchaseOrderListByFinishAjax(int finish) {
		return orderService.selectByFinish(finish);
	}

	@RequestMapping("getPurchaseOrderListBySupplierIdAjax")
	@ResponseBody
	public List<PurchaseOrder> getPurchaseOrderListBySupplierIdAjax(HttpServletRequest request) {
		int userId=((User)request.getSession().getAttribute("userInfo")).getUserId();
		return orderService.selectBySupplierId(userId);
		
	}

	/**
	 * 确认发货
	 * 
	 * @param purchaseOrderId
	 * @param state
	 *            4确认发货 5拒绝发货
	 * @return
	 */
	@RequestMapping("sendMaterialAjax")
	@ResponseBody
	public int sendMaterialAjax(int purchaseOrderId, String state) {
		purchaseOrder = new PurchaseOrder();
		purchaseOrder.setPurchaseOrderId(purchaseOrderId);

		purchaseOrder.setState(state);
		if ("5".equals(state)) {
			purchaseOrder.setFinish(1);
		} else {
			purchaseOrder.setFinish(0);
			purchaseOrder.setSendMaterialTime(TimeUtil.formatTime());
		}
		return orderService.sendMaterial(purchaseOrder);
	}

	/**
	 * 确认收货
	 * 
	 * @param purchaseOrderId
	 * @param state
	 *            6确认收货 7拒绝收货
	 * @return
	 */
	@RequestMapping("receiveMaterialAjax")
	@ResponseBody
	public int receiveMaterialAjax(int purchaseOrderId, String state) {
		purchaseOrder = new PurchaseOrder();
		purchaseOrder.setPurchaseOrderId(purchaseOrderId);
		purchaseOrder.setReceiveMaterialTime(TimeUtil.formatTime());
		purchaseOrder.setState(state);
		purchaseOrder.setFinish(1);
		// 下面处理确认收货后 就把原料放入库存待入库的状态
		if ("6".equals(state)) {
			List<PurchaseOrderMaterial> list = orderMaterialService
					.selectByPurchaseOrderId(purchaseOrderId);
			Stock stock;
			for (PurchaseOrderMaterial p : list) {
				stock = new Stock();
				stock.setState(0);// 0为待入库
				stock.setMaterialId(p.getMaterialId());
				stock.setQuantity(Double.parseDouble(p.getMaterialQuantity()));
				stockService.insert(stock);
			}
		}

		return orderService.receiveMaterial(purchaseOrder);
	}

}
